<?php

/*
 * 子订单类
 */

namespace Home\Logic;

class OrderinfoLogic {

    public function __construct() {
        $this->order_info = D('Action_order_info');
        $this->item = new \Home\Logic\ShopitemLogic();
    }
    
    public function setOrderId($order_id){
        $this->order_info->setTableNamePre($order_id);
    }
    
    public function getOrderInfo($order_id,$store_id){
        if($order_id<1){
            throw new \Exception('order_id is null');
        }
        $this->setOrderId($order_id);
        $orders = $this->order_info->where('order_id='.$order_id)->select();
        if($orders!=false){
            $item = new \Home\Logic\ShopitemLogic();
            foreach($orders as $k=>$v){
                if($v['type']!=0){
                    if($v['type']!=$type){
                        $_item = $item->getItemById($v['type'],$store_id,true);
                        $type = $v['type'];
                    }
                }else{
                    $_item = $item->getItemById($v['item_id'],$store_id,true);
                }
                
                $orders[$k]['item'] = $_item;
            }
        }
        return $orders;
    }
    
    public function insertOrderInfo($data){
        if($data['order_id']<1){
            throw new \Exception('order_id is null');
        }
        $this->setOrderId($data['order_id']);
        return $this->order_info->add($data);
    }
    
    /*数量减一 加一*/
    public function updateNumber($id,$order_id,$count){
        if($order_id<1){
            throw new \Exception('order_id is null');
        }
        $this->setOrderId($order_id);
        return $this->order_info->where('id='.$id)->save(array('count'=>$count));
    }
    
    public function getOrderInfoById($id,$order_id){
        if($order_id<1){
            throw new \Exception('order_id is null');
        }
        $this->setOrderId($order_id);
        return $this->order_info->where('id='.$id)->find();
    }
}